package cn.maihe.elg.operation.web.business;

import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.vo.business.CreateInstitutionVO;
import cn.maihe.elg.operation.model.vo.business.UpdateInstitutionVO;
import cn.maihe.elg.operation.repository.entity.GuaranteeInstitutionDO;
import cn.maihe.elg.operation.repository.entity.GuaranteeInstitutionDOCondition;
import cn.maihe.elg.operation.repository.entity.GuaranteeInstitutionVo;
import cn.maihe.elg.operation.service.business.InstitutionService;
import cn.maihe.elg.operation.utils.IdGenerate;
import cn.maihe.framework.starter.base.sql.PageBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description 出函机构controller
 * @Author WJH
 * @Date 2021/03/25
 */
@Api(value = "出函机构管理", tags = "出函机构管理")
@RestController
@RequestMapping("/api")
@Slf4j
public class InstitutionController {
    @Resource
    private InstitutionService institutionService;

    @ApiOperation(value = "分页查询出函机构", notes = "根据出函机构类型、机构编码、机构名称 分页查询")
    @RequestMapping(value = "/institutions", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "institutionType", value = "出函机构类型", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "institutionCode", value = "出函机构编码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "guarantorName", value = "出函机构名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "guarantorCreditCode", value = "统一社会信用代码", dataType = "String", paramType = "query")
    })
    public PageBo<GuaranteeInstitutionDO> pageList(int pageNo, int pageSize, GuaranteeInstitutionVo vo) {
        return institutionService.selectPageBySql(pageNo, pageSize, vo);
    }

    @ApiOperation(value = "获取出函机构", notes = "根据出函机构id获取出函机构")
    @ApiImplicitParam(name = "id", value = "出函机构id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/institution/{id}", method = RequestMethod.GET)
    public GuaranteeInstitutionDO getInstitution(@PathVariable Long id) {
        GuaranteeInstitutionDO curDO = institutionService.selectById(id);
        if (curDO == null) {
            throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "出函机构id");
        }
        return curDO;
    }

    @ApiOperation(value = "新增出函机构", notes = "新增出函机构")
    @RequestMapping(value = "/institution", method = RequestMethod.POST)
    public void createInstitution(@Validated @RequestBody CreateInstitutionVO vo) {
        GuaranteeInstitutionDO cDO = new GuaranteeInstitutionDO();
        BeanUtils.copyProperties(vo, cDO);
        cDO.setId(IdGenerate.getId());
        if (institutionService.exists(GuaranteeInstitutionDOCondition.builder().andInstitutionCodeEq(vo.getInstitutionCode()).build())) {
            throw new SystemRuntimeException(ErrorCode.EXISTS, "出函机构编码", vo.getInstitutionCode());
        }
        if (institutionService.exists(GuaranteeInstitutionDOCondition.builder().andGuarantorNameEq(vo.getGuarantorName()).build())) {
            throw new SystemRuntimeException(ErrorCode.EXISTS, "出函机构名称", vo.getGuarantorName());
        }
        institutionService.insert(cDO);
    }

    @ApiOperation(value = "修改出函机构", notes = "根据出函机构id修改出函机构")
    @RequestMapping(value = "/institution", method = RequestMethod.PUT)
    public void modifyInstitution(@Validated @RequestBody UpdateInstitutionVO vo) {
        GuaranteeInstitutionDO mDO = new GuaranteeInstitutionDO();
        BeanUtils.copyProperties(vo, mDO);

        if (institutionService.exists(GuaranteeInstitutionDOCondition.builder().andGuarantorNameEq(vo.getGuarantorName()).andIdNe(vo.getId()).build())) {
            throw new SystemRuntimeException(ErrorCode.EXISTS, "出函机构名称", vo.getGuarantorName());
        }
        institutionService.updateById(mDO);
    }

    @ApiOperation(value = "删除出函机构", notes = "根据出函机构id删除出函机构")
    @ApiImplicitParam(name = "id", value = "出函机构id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/institution/{id}", method = RequestMethod.DELETE)
    public void deleteInstitution(@PathVariable Long id) {
        if (institutionService.existsInstitutionLinkedUser(id)) {
            throw new SystemRuntimeException(ErrorCode.NONDELETE, "出函机构已存在关联用户");
        }
        institutionService.deleteById(id);
    }


}
